package com.example.bootcurriculumproject.testController;

import com.example.bootcurriculumproject.entity.User;
import com.example.bootcurriculumproject.mapper.UserMapper;
import com.example.bootcurriculumproject.service.impl.UserService;
import com.example.bootcurriculumproject.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;


@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@Slf4j
public class testUserController {
    @SpyBean
    private UserService userService1;
    @MockBean
    private UserService userService;
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private RedisUtil redisUtil;
    @MockBean
    private UserMapper userMapper;
    private static final String URL = "/user";
    @Test
    public void testAdd() throws Exception {//无返回值
        User user = new User("bbd0","aadd",0,"aaggee",23355,"rrttyy","112333","2233");
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        //控制类测试
        Mockito.doNothing().when(userService1).insertUser(any());
        String body = "{\"username\": \"aaaaaa\",\"password\": \"测试\",\"status\": 0,\"address\": \"测试\",\"phone\": 23355,\"image\": \"bbccdd\",\"nickname\": \"kkbb\"}";
        //  String exceptReturn = "{\"id\":10,\"question\":\"ssss的问题1\",\"answer\":\"回答问题\"}";
        String result = this.mockMvc.perform(MockMvcRequestBuilders.post("/user/add")
                .accept(MediaType.APPLICATION_JSON)
                .content(body.getBytes("utf-8"))
                .contentType(MediaType.APPLICATION_JSON_VALUE)
        ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testUpdateUser() throws Exception {
        User user = new User("bbd0","aadd",0,"aaggee",23355,"rrttyy","112333","2233");
        //登录打桩1212
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.doNothing().when(userService1).updateUser(any());
        String body = "{\"username\": \"aaaaaa\",\"password\": \"测试\",\"status\": 0,\"address\": \"测试\",\"phone\": 23355,\"image\": \"bbccdd\",\"nickname\": \"kkbb\"}";
        String result = this.mockMvc.perform(MockMvcRequestBuilders.put(URL+"/update")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testDelete() throws Exception {
        User user = new User("bbd0","aadd",0,"aaggee",23355,"rrttyy","112333","2233");
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.doNothing().when(userService1).deleteUser(any());
        //String body = "{\"username\":\"bbbbbb\",\"password\":测试1,\"status\":测试1,\"address\":测试1,\"phone\":111111,\"image\":\"cccccc,\"nickname\":\"dddd\"";
        String result = this.mockMvc.perform(MockMvcRequestBuilders.delete(URL+"/delete/1122")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public  void testLogin() throws Exception {

        User user = new User("bbd0","aadd",0,"aaggee",23355,"rrttyy","112333","2233");
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(userService1.selectByUid(any())).thenReturn(user);
        Mockito.when(redisUtil.set(Mockito.anyString(),Mockito.any(),Mockito.anyLong())).thenReturn(true);
        Mockito.when(userService1.updateStatus(user.getUid(),1)).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.get(URL+"/login?username=bbb0&password=aadd")
                        .accept(MediaType.APPLICATION_JSON_VALUE)

                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
              assertEquals("登录成功",result);
    }

    @Test
    public void testRegister() throws Exception {
        String body = "{\"username\": \"aaaaaa\",\"password\": \"测试\",\"status\": 0,\"address\": \"测试\",\"phone\": 23355,\"image\": \"bbccdd\",\"nickname\": \"kkbb\"}";
        User user = new User("bbd0","aadd",1,"aaggee",23355,"rrttyy","112333","2233");
        Mockito.doNothing().when(userService1).insertUser(any());
        String result = this.mockMvc.perform(MockMvcRequestBuilders.post(URL+"/register")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }

}
